﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;
using System.Text;
using System.Data;
using Portal.Logic;

namespace Portal
{
    public partial class MenuManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if( !IsPostBack){
                BindDropDownList();
            }
        }

        //绑定菜单下拉框
        void BindDropDownList() {

            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"select t.cnname,t.resourceid,t.ext07 from COM_RESOURCE t where t.type='Menu'");
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSQL.ToString());
            ddlMenu.Items.Add(new ListItem("--请选择--",""));
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                ddlMenu.Items.Add(new ListItem(dt.Rows[i]["cnname"].ToString(), dt.Rows[i]["resourceid"].ToString()));
            }

            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("select * from com_resource c where c.resourceid=7 or c.resourceid=18 or c.resourceid=13 or c.resourceid=26");
            DataTable dtParent = DataAccess.Instance("BizDB").ExecuteDataTable(sbSql.ToString());
            ddlParent.Items.Add(new ListItem("--请选择--", ""));
            for (int i = 0; i < dtParent.Rows.Count; i++)
            {
                ddlParent.Items.Add(new ListItem(dtParent.Rows[i]["cnname"].ToString(), dtParent.Rows[i]["resourceid"].ToString()));
            }
        }

        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //获取页面数据
            string me = txtMenuEngin.Text.Trim();
            string mn = txtMenuName.Text.Trim();
            string tp = txtPath.Text.Trim();
            string parent = ddlParent.SelectedItem.Value.Trim();

            //检测是否名称重复
            DataTable dtName = DataAccess.Instance("BizDB").ExecuteDataTable("select * from com_resource c where  c.type='Menu' and c.cnname ='" + mn + "'");
            if(dtName.Rows.Count>0){
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('不能使用同名的菜单命名!');location.href='MenuManage.aspx';", true);
                return;
            }
            
            //生成id
            SerialNoLogic sn = new SerialNoLogic();
            int id = sn.GetSerialNo("COM_Resource");

            //生成orderid
            int OrderId;
            DataTable dtOrder = DataAccess.Instance("BizDB").ExecuteDataTable("select max(c.orderno) from com_resource c");
            if (string.IsNullOrEmpty(txtOrderID.Text.Trim()))
            {

                OrderId = Convert.ToInt32(dtOrder.Rows[0][0]) + 1;
            }
            else {

                OrderId = Convert.ToInt32(txtOrderID.Text.Trim());
            }
           

            //判断是否为子菜单
            string content = "";
            if (parent != "0")
            {
                content = "content";
            }

            if(string.IsNullOrEmpty(parent)){
                parent = "0";
            }

            //插入数据
            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"insert into com_resource c (c.resourceid,c.cnname,c.enname,c.type,c.parentid,c.isactive,c.ext01,c.orderno,c.ext02) values ({0},'{1}','{2}','Menu',{3},1,'{4}',{5},'{6}')", id, mn, me, parent, tp, OrderId, content);
            int num = DataAccess.Instance("BizDB").ExecuteNonQuery(StrSQL.ToString());
            if (num > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('添加成功!');location.href='MenuManage.aspx';", true);
            }
        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //获取页面数据
            string id = lblId.Text.Trim();
            
            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"delete com_resource r where r.resourceid={0} or r.parentid={0}", id);
            int num = DataAccess.Instance("BizDB").ExecuteNonQuery(StrSQL.ToString());
            DataAccess.Instance("BizDB").ExecuteNonQuery("delete com_resourcerelation c where c.ext02="+id);
            if(num>0){
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('删除成功!');location.href='MenuManage.aspx';", true);
            }
        }

        //更新
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //获取页面数据
            string me = txtMenuEngin.Text.Trim();
            string mn = txtMenuName.Text.Trim();
            string tp = txtPath.Text.Trim();
            string id = lblId.Text.Trim();
            int parent;
            if (string.IsNullOrEmpty(ddlParent.SelectedItem.Value.Trim()))
            {
                parent = 0;
            }
            else {
                parent = Convert.ToInt32(ddlParent.SelectedItem.Value.Trim());
            }

            //查询orderid
             int OrderId ;
            DataTable dtOrder = DataAccess.Instance("BizDB").ExecuteDataTable("select c.orderno from com_resource c where c.resourceid=" + id);
            if (string.IsNullOrEmpty(txtOrderID.Text.Trim()))
            {

                OrderId = Convert.ToInt32(dtOrder.Rows[0][0]) + 1;
            }
            else
            {

                OrderId = Convert.ToInt32(txtOrderID.Text.Trim());
            }


            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"update com_resource x set x.cnname='{0}',x.enname='{1}',x.type='Menu',x.parentid={2},x.isactive=1,x.ext01='{3}',x.orderno={4} where x.resourceid={4}", mn, me, parent, tp, id, OrderId);
            int num = DataAccess.Instance("BizDB").ExecuteNonQuery(StrSQL.ToString());
            if (num > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('更新成功!');location.href='MenuManage.aspx';", true);
            }
        }

        //选择菜单事件
        protected void ddlMenu_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        //选择菜单事件
        protected void ddlMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //给Id赋值
            lblId.Text = ddlMenu.SelectedItem.Value.Trim();

            txtMenuName.Text = ddlMenu.SelectedItem.Text.Trim();
            StringBuilder StrSQL = new StringBuilder();
            StrSQL.AppendFormat(@"select * from com_resource k where k.resourceid={0}", Convert.ToInt32(lblId.Text));
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSQL.ToString());
            txtMenuEngin.Text = dt.Rows[0]["enname"].ToString();
            txtPath.Text = dt.Rows[0]["ext01"].ToString();
            txtOrderID.Text = dt.Rows[0]["orderno"].ToString();
            string parentid = dt.Rows[0]["parentid"].ToString().Trim();
            ddlMenu.Items.Clear();
            ddlParent.Items.Clear();
            BindDropDownList();

            //string str = "select * from com_resource x where x.resourceid=" + parentid;
            //DataTable dts = DataAccess.Instance("BizDB").ExecuteDataTable(str);
            
            //为父菜单添加默认项
            for (int i = 0; i < ddlParent.Items.Count;i++ ) {

                if (parentid.Trim() == ddlParent.Items[i].Value.Trim())
                {
                    ddlParent.SelectedItem.Value = ddlParent.Items[i].Value;
                    ddlParent.SelectedItem.Text = ddlParent.Items[i].Text;
                }
            }
          
        }
    }
}